<?php

/*
 *
 * -------------------------------------------------------
 * CLASSNAME:        city
 * FOR MYSQL TABLE:  city
 * FOR MYSQL DB:     yogofly
 * -------------------------------------------------------
 * -------------------------------------------------------
 *
 */

class city {

    /**
     *   @desc Variable Declaration with default value
     */
    protected $_id;   // KEY ATTR. WITH AUTOINCREMENT
    protected $_name;
    protected $_state;
    protected $_country;
    protected $_driver_fee;
    protected $_position;
    protected $_is_active;

    /**
     *   @desc   CONSTRUCTOR METHOD
     */
    function __construct() {
        global $DBOBJ;
        $this->_obj = $DBOBJ;
        $this->_id = null;
        $this->_name = null;
        $this->_state = null;
        $this->_country = null;
        $this->_driver_fee = null;
        $this->_position = null;
        $this->_is_active = null;
    }

    /**
     *   @desc   DECONSTRUCTOR METHOD
     */
    function __destruct() {
        unset($this->_obj);
    }

    /**
     *   @desc   GETTER METHODS
     */
    public function getid() {
        return $this->_id;
    }

    public function getname() {
        return $this->_name;
    }

    public function getstate() {
        return $this->_state;
    }

    public function getcountry() {
        return $this->_country;
    }

    public function getdriver_fee() {
        return $this->_driver_fee;
    }

    public function getposition() {
        return $this->_position;
    }

    public function getis_active() {
        return $this->_is_active;
    }

    /**
     *   @desc   SETTER METHODS
     */
    public function setid($id) {
        $this->_id = $id;
    }

    public function setname($name) {
        $this->_name = $name;
    }

    public function setstate($state) {
        $this->_state = $state;
    }

    public function setcountry($country) {
        $this->_country = $country;
    }

    public function setdriver_fee($driver_fee) {
        $this->_driver_fee = $driver_fee;
    }

    public function setposition($position) {
        $this->_position = $position;
    }

    public function setis_active($is_active) {
        $this->_is_active = $is_active;
    }

    /**
     *   @desc   SELECT METHOD / LOAD
     */
    public function select($id) {
        $sql = "SELECT * FROM city WHERE id = $id";
        $row = $this->_obj->select($sql);

        if (count($row) > 0) {
            $this->_id = $row[0]['id'];
            $this->_name = $row[0]['name'];
            $this->_state = $row[0]['state'];
            $this->_country = $row[0]['country'];
            $this->_driver_fee = $row[0]['driver_fee'];
            $this->_position = $row[0]['position'];
            $this->_is_active = $row[0]['is_active'];
            return $row[0];
        } else {
            return;
        }
    }

    /**
     *   @desc   set All
     */
    public function setAll($dataArray = array()) {
        if (count($dataArray) > 0) {
            return;
        }

        foreach ($dataArray as $key => $value) {
            $classKey = '_' . $key;
            $this->$classKey = $value;
        }
    }

    /**
     *   @desc   get All
     */
    public function getAll($FORMAT = 'ARRAY') {
        $dataArray = new stdClass();
        $dataArray->id = $this->getid();
        $dataArray->name = $this->getname();
        $dataArray->state = $this->getstate();
        $dataArray->country = $this->getcountry();
        $dataArray->driver_fee = $this->getdriver_fee();
        $dataArray->position = $this->getposition();
        $dataArray->is_active = $this->getis_active();
        if ($FORMAT == 'ARRAY') {
            return (array) $dataArray;
        }
        return $dataArray;
    }

    /**
     *   @desc   COUNT
     */
    public function getCountcity() {
        $sql = "SELECT count(*) as cnt  FROM city";
        $result = $this->_obj->select($sql);
        return $result['0']['cnt'];
    }

    /**
     *   @desc   DELETE
     */
    public function delete($id) {
        $sql = "DELETE FROM city WHERE id = $id";
        $result = $this->_obj->sql_query($sql);
    }

    /**
     *   @desc   INSERT
     */
    public function insert() {
        $this->id = ""; // clear key for autoincrement
        $sql = "INSERT INTO city ( id,name,state,country,driver_fee,position,is_active ) VALUES ( NULL,'" . $this->_name . "','" . $this->_state . "','" . $this->_country . "','" . $this->_driver_fee . "','" . $this->_position . "','" . $this->_is_active . "' )";
        $result = $this->_obj->insert($sql);
        return $result;
    }

    /**
     *   @desc   UPDATE
     */
    public function update($id) {
        $sql = " UPDATE city SET  id = '" . $this->_id . "' , name = '" . $this->_name . "' , state = '" . $this->_state . "' , country = '" . $this->_country . "' , driver_fee = '" . $this->_driver_fee . "' , position = '" . $this->_position . "' , is_active = '" . $this->_is_active . "'  WHERE id = $id ";
        $result = $this->_obj->sql_query($sql);
    }

    //Get City Data
    public function getcity($arguments = array()) {
          $id = $arguments['id'];
          $citys = array();
          if ($id != '') {
               $where = " where id = '" . $id . "'";
          } else {
               $where = '';
          }
          $orderby = ' ORDER BY id asc';
          $sql = "SELECT id,name FROM city" . $where. $orderby;
          $row = $this->_obj->select($sql);
          $count = '0';
          if ($row) {
               $JSONARRAY = Array(
                   'success' => true,
                   'message' => '',
                   'total' => count($row),
                   'data' => $row
               );
               $jsonString = json_encode($JSONARRAY);
               return $jsonString;
          }
          $JSONARRAY = Array(
              'success' => false,
              'message' => 'No such data found in database with this id',
              'total' => $count,
          );
          $jsonString = json_encode($JSONARRAY);
          return $jsonString;
     }

}

?>